#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
 char * message;//处理的消息
 int prior;//优先级
};
typedef struct node * Node;
struct minHeap//最小堆
{
 Node d;
 int size;
 int maxsize;
};
typedef struct minHeap * MinHeap;
MinHeap Insert(MinHeap M, char * c, int n);//往最小堆内插入元素
struct node Delete(MinHeap M);//从最小堆内删除元素
int main()
{
 int N;
 scanf("%d", &N);
 MinHeap M;
 M = (MinHeap)malloc(sizeof(struct minHeap));
 M->d = (Node)malloc((N + 1) * sizeof(struct node));
 M->size = 0;
 M->maxsize = 100000;
 int i;
 M->d[0].message = (char *)malloc(11 * sizeof(char));
 M->d[0].prior = -1;
 char *ch;
 ch = (char *)malloc(11 * sizeof(char));
 char c;
 scanf("%c", &c);//读入换行符
 int d;
 struct node n;
 for (i = 0; i < N; i++)
 {
  scanf("%s", ch);
  if (strcmp(ch, "PUT") == 0)
  {
   scanf("%c", &c);//读入空格
   scanf("%s", ch);
   scanf("%d\n", &d);
   M = Insert(M, ch, d);
  }
  else if (strcmp(ch, "GET") == 0)
  {
   if (M->size == 0)
   {
    printf("EMPTY QUEUE!\n");
   }
   else {
    n = Delete(M);
    printf("%s\n", n.message);
   }
  }
 }
 return 0;
}
//往最小堆内插入元素
MinHeap Insert(MinHeap M, char * c, int n)
{
 int i;
 if (M->size == 0)
 {
  i = ++M->size;
  M->d[i].message = (char *)malloc(10 * sizeof(char));
  strcpy(M->d[i].message, c);
  M->d[i].prior = n;
 }
 else
 {
  i = ++M->size;
  while (M->d[i / 2].prior > n)
  {
   M->d[i] = M->d[i / 2];
   i = i / 2;
  }
  M->d[i].message = (char *)malloc(11 * sizeof(char));
  strcpy(M->d[i].message, c);
  M->d[i].prior = n;
 }
 return M;
}
//从最小堆内删除元素
struct node Delete(MinHeap M)
{
 int j;
 struct node n;
 n = M->d[1];
 j = 2;
 while (j < M->size)
 {
  if (M->d[j].prior > M->d[j + 1].prior)
  {
   j = j + 1;
  }
  if (M->d[j].prior < M->d[M->size].prior)
  {
   M->d[j / 2] = M->d[j];
  }
  else { break; }
  j = 2 * j;
 }
 M->d[j / 2] = M->d[M->size];
 M->size--;
 return n;
}